package com.example.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：操作用户功能
 * 作者：zhoujun
 * 日期： 2024/4/23 1:39
 */


@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    @Resource
    public UserMapper userMapper;

    @Override
    public boolean save(User entity) {
        if(StrUtil.isBlank(entity.getName())) {
            entity.setName(entity.getName());
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123");
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("用户");
        }
        return super.save(entity);
    }

    @Override
    public boolean removeById(User entity) {
        User currentUser = TokenUtils.getCurrentUser();
        if(currentUser.getId().equals(entity.getId())){
            throw new ServiceException("不能删除当前的用户信息！");
        }
        return super.removeById(entity);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        User currentUser = TokenUtils.getCurrentUser();
        if(currentUser != null && currentUser.getId() != null && list.contains(currentUser.getId())){
            throw new ServiceException("不能删除当前的用户");

        }
        return super.removeBatchByIds(list);
    }

    public User selectByUsername(String username){
       QueryWrapper<User> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("username", username);
      return getOne(queryWrapper);

   }

    public User login(User user) {

        User dbUser = selectByUsername(user.getUsername());



        if (dbUser == null) {
            throw new ServiceException("用户名或密码错误！");
        }
        if (!user.getUsername().equals(dbUser.getUsername())) {
            throw new ServiceException("用户名或密码错误");
        }

        if(!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误！");
        }
        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);

        return dbUser;
    }

    public User register(User user) {
        User dbUser = selectByUsername(user.getUsername());
        if(dbUser != null)  throw new ServiceException("用户名已存在！");
        user.setName(user.getUsername());
        userMapper.insert(user);
        return user;
    }

    public void resetPassword(User user) {

        User dbUser = selectByUsername(user.getUsername());
        if(dbUser == null){
            throw new ServiceException("用户不存在！");
        }
        if(!user.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误！");
        }
        dbUser.setPassword("123");
        updateById(dbUser);
    }




}